JavaScript Data Structures and Algorithms by Sammie Bae
Author: Sammie Bae , Date: January 27, 2019
, Views: 420
Time Complexity: O(1)
Insertion
Inserting into a stack can be done via the push function natively supported with JavaScript arrays.1 Stack.prototype.push = function(value){
2 this.array.push(value);
3 }
4
5 stack1.push(1);
6 stack1.push(2);
7 stack1.push(3);
8 console.log(stack1); // {array: [1,2,3]}
Time Complexity: O(1)
Deletion
Deletion can also be implemented using a native JavaScript array method, called pop.1 Stack.prototype.pop = function() {
2 return this.array.pop();
3 };
4
5 stack1.pop(1);
6 stack1.pop(2);
7 stack1.pop(3);
8
9 console.log(stack1); // {array: []}
Copyright Disclaimer:
This site does not store any files on its server. We only index and link
to content provided by other sites. Please contact the content providers
to delete copyright contents if any and email us, we'll remove relevant
links or contents immediately.